package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import fl.controls.RadioButton;
	
	[SWF(width="500",height="425",frameRate="30",backgroundColor="0xFFFFFF")]
	
	/**
	 * ...
	 * @author WLDragon
	 */
	public class Main extends Sprite
	{
		
		public function Main():void
		{
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.doubleClickEnabled = true;
			stage.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
			
			//初始化
			mAstar = new AStar;
			mMapPanel = new Sprite;
			mMapPanel.y = 23;
			
			mPathPanel = new Sprite;
			
			mTxt = new TextField;
			mTxt.width = 217;
			mTxt.height = 23;
			mTxt.textColor = 0xFF0000;
			mTxt.text = "点击地图进行寻路(双击空白处改变地图)";
			
			mPlayer = new Sprite;
			
			mPlayer.graphics.beginFill(0xFF0000);
			mPlayer.graphics.drawRect(0, 0, mBlockSize, mBlockSize);
			mPlayer.graphics.endFill();
			
			/*mRadio4 = new RadioButton;
			mRadio4.label = "4方向";
			mRadio4.selected = true;
			mRadio4.x = 380;
			
			mRadio8 = new RadioButton;
			mRadio8.label = "8方向";
			mRadio8.x = 440;*/
			
			//添加到显示列表
			mMapPanel.addChild(mPathPanel);
			mMapPanel.addChild(mPlayer);
			
			addChild(mMapPanel);
			addChild(mTxt);
//			addChild(mRadio4);
//			addChild(mRadio8);
			
			//添加侦听事件
			mMapPanel.addEventListener(MouseEvent.CLICK, onClick);
			
			drawMap();
		}
		
		private function onDoubleClick(e:MouseEvent):void
		{
			mPathPanel.graphics.clear();
			drawMap();
		}
		
		private function onClick(e:MouseEvent):void
		{
			//转换终点坐标为地图索引
			var x1:int = mPlayer.x / mBlockSize >> 0;
			var y1:int = mPlayer.y / mBlockSize >> 0;
			var x2:int = e.localX / mBlockSize >> 0;
			var y2:int = e.localY / mBlockSize >> 0;
			
			if (mAstar.canReach(x2, y2))
			{
				//寻路
				var d:int = 8;
				var t:int = getTimer();
				mPath = mAstar.find([x1, y1], [x2, y2], d);
				t = getTimer() - t;
				if (mPath != null)
				{
					mTxt.text = "用时" + t + "毫秒(双击空白处改变地图)";
					mPathPanel.graphics.clear();
					//路存在，侦听每侦事件，画路
					addEventListener(Event.ENTER_FRAME, handleFrame);
				}
				else
				{
					mTxt.text = "不可达" + t + "毫秒(双击空白处改变地图)";
				}
			}
			else
			{
				mTxt.text = "目标不可达(双击空白处改变地图)";
			}
		}
		
		private function handleFrame(e:Event):void
		{
			if (mPath.length == 0)
			{
				removeEventListener(Event.ENTER_FRAME, handleFrame);
				return;
			}
			
			drawPath();
		}
		
		/**
		 * 产生随机地图数据和画地图
		 */
		private function drawMap():void
		{
			mMap = [];
			var isClog:Boolean;
			mMapPanel.graphics.clear();
			
			for (var i:int = 0; i < mRows; i++)
			{
				mMap[i] = [];
				for (var j:int = 0; j < mCols; j++)
				{
					isClog = Math.random() < 0.3 ? true : false;
					mMapPanel.graphics.beginFill(isClog?0:0xCCCCCC);
					
					mMapPanel.graphics.drawRect(mBlockSize * j, mBlockSize * i, mBlockSize, mBlockSize);
					mMapPanel.graphics.endFill();
					mMap[i][j] = isClog ? 0 : 1;
				}
			}
			
			mAstar.setMap(mMap);
		}
		
		private function drawPath():void
		{
			//提取节点
			var n:Array = mPath.pop();
			var tx:int = n[0] * mBlockSize;
			var ty:int = n[1] * mBlockSize;
			//移动玩家方块
			mPlayer.x = tx;
			mPlayer.y = ty;
			//画路
			mPathPanel.graphics.beginFill(0xFF0000, 0.5);
			mPathPanel.graphics.drawRect(tx, ty, mBlockSize, mBlockSize);
			mPathPanel.graphics.endFill();
		}
		
		/**提示文本*/
		private var mTxt:TextField;
		/**地图数据*/
		private var mMap:Array;
		/**地图显示*/
		private var mMapPanel:Sprite;
		/**地图的列数*/
		private var mCols:int = 50;
		/**地图的行数*/
		private var mRows:int = 40;
		/**地图上正方形方块的大小*/
		private var mBlockSize:int = 10;
		/**玩家方块*/
		private var mPlayer:Sprite;
		/**路径显示*/
		private var mPathPanel:Sprite;
		/**当前寻到的路*/
		private var mPath:Array;
		/**8方向A*寻路*/
		private var mAstar:AStar;
		/**选择4方向*/
		private var mRadio4:RadioButton;
		/**选择8方向*/
		private var mRadio8:RadioButton;
	}
	
}